package com.blockmeta.bbs.businesslibrary.umeng;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import androidx.annotation.IdRes;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.permission.RequestPermissionUtil;
import com.blockmeta.bbs.businesslibrary.R;

/**
 * @author AirThor
 * @date 2020/3/23.
 */
public abstract class BaseShareDialog implements View.OnClickListener {
    /** APK 下载链接 */
    protected static final String sUrl = BaseApp.getApp().getResources().getString(R.string.apk_download_url);
    protected AlertDialog mDialog;
    protected ShareRouter mShareRouter;
    protected Context mContent;

    public BaseShareDialog(Context context,ShareRouter shareRouter) {
        mContent = context;
        mShareRouter = shareRouter;
        mDialog = new AlertDialog.Builder(context, R.style.AlertDialog).create();
        mDialog.show();
        mDialog.setContentView(setContentViews());
        setAttributes();
        initContent();
    }

    /** 找到具体控件 初始化 */
    protected abstract void initContent();

    /** 设置dialog布局文件*/
    protected abstract int setContentViews();
    /**
     * 设置dialog宽高 Gravity属性
     * 可调用{@link #setAttribute(int, int, int)}快速设置
     */
    protected abstract void setAttributes();

    /**
     * 设置尺寸属性
     * @param width {@link WindowManager.LayoutParams}
     * @param height {@link WindowManager.LayoutParams}
     * @param gravity {@link Gravity}
     */
    protected void setAttribute(int width, int height,int gravity){
        try {
            WindowManager.LayoutParams layoutParams = mDialog.getWindow().getAttributes();
            layoutParams.width = width;
            layoutParams.height = height;
            layoutParams.gravity = gravity;
            mDialog.getWindow().setAttributes(layoutParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * java.lang.IllegalArgumentException: View=DecorView@xxxx[xx] not attached to window manager
     * 出现该错误的原因是在dialog调用dismiss方法时，activity已经销毁了，使得dialog无法获取activity对应的window manager进而抛出异常。
     * 这里做吃掉异常
     */
    protected void safeDismiss(){
        try {
            mDialog.dismiss();
            if(mContent instanceof Activity){
                Activity activity = (Activity) this.mContent;
                activity.finish();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(final View view) {
        RequestPermissionUtil.requestStorage(new RequestPermissionUtil.OnPermissionGrantedListener() {
            @Override
            public void onPermissionGranted() {
                int i = view.getId();
                if (i == R.id.iv_wechat) {
                    shareInterceptor(MEDIA_TYPE.WEIXIN);
                } else if (i == R.id.iv_wechat_circle) {
                    shareInterceptor(MEDIA_TYPE.WEIXIN_CIRCLE);
                } else if (i == R.id.iv_long_pic) {
                    shareInterceptor(MEDIA_TYPE.SAVE);
                } else if (i == R.id.iv_qq) {
                    shareInterceptor(MEDIA_TYPE.QQ);
                } else if (i == R.id.iv_weibo) {
                    shareInterceptor(MEDIA_TYPE.SINA);
                } else if (i == R.id.tv_dismiss) {
                    safeDismiss();
                }else if(i == R.id.tv_cancel){
                    safeDismiss();
                }else if (i == R.id.iv_im_circle){
                    shareInterceptor(MEDIA_TYPE.IM);
                }
            }
        });
    }

    private void shareInterceptor(MEDIA_TYPE mediaType){

        OnShareClickListener onShareClickListener = mShareRouter.getOnShareClickListener();
        if (onShareClickListener!=null){
            onShareClickListener.onShareClickListener(mediaType);
        }

        startShare(mediaType);
    }
    /**
     * 开始分享
     * @param mediaType 分享的媒体类型
     */
    protected abstract void startShare(MEDIA_TYPE mediaType);

    protected <T extends View> T find(@IdRes int id){
        return  mDialog.findViewById(id);
    }
}
